package com.ztstkj.lzscale.fragments

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.ztstkj.lzscale.R
import com.ztstkj.lzscale.databinding.FragmentUserBinding
import com.ztstkj.lzservices.dto.UserOutput

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [UserFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class UserFragment(var user: UserOutput?) : Fragment() {

    private var param1: String? = null
    private var param2: String? = null
    private lateinit var binding: FragmentUserBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding= FragmentUserBinding.inflate(inflater,container,false)//不起作用
        //binding= DataBindingUtil.inflate(inflater,container,false)
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_user, container, false)
    }

    @SuppressLint("SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        //DebugUtil.warnOut("user:${GsonUtils.toJson(user)}")
        val fragLUser=view.findViewById<LinearLayout>(R.id.fragLUser)
        val fragLUserInfo=view.findViewById<LinearLayout>(R.id.fragLUserInfo)
        val fragLlUserAddress=view.findViewById<LinearLayout>(R.id.fragLlUserAddress)
        val fragTvUserCard=view.findViewById<TextView>(R.id.fragTvUserCard)
        val fragTvUserPoint=view.findViewById<TextView>(R.id.fragTvUserPoint)
        val fragTvUserBuilding=view.findViewById<TextView>(R.id.fragTvUserBuilding)
        val fragTvUserAddress=view.findViewById<TextView>(R.id.fragTvUserAddress)
        val frageImgAvatar=view.findViewById<ImageView>(R.id.frageImgAvatar)
        if(user!=null){
            fragLUser.setBackgroundResource(R.drawable.main_block_bg)
            fragLUserInfo.visibility = ViewGroup.VISIBLE
            fragLlUserAddress.visibility = ViewGroup.VISIBLE
            fragTvUserCard.text = "卡号：${user?.cardNo}"
            fragTvUserPoint.text = user?.totalPoint.toString()
            if(user?.nickName!=null){
                fragTvUserBuilding.text = user?.nickName
            }else{
                fragTvUserBuilding.text = user?.realName
            }
            fragTvUserAddress.text = "地址：${user?.detailedAddress?:"无"}"
            if (user?.avatar != null) {
                Glide.with(this)
                    .load(user?.avatar)
                    .transform(RoundedCorners(16))
                    .into(frageImgAvatar)
            } else {
                frageImgAvatar.setImageResource(R.drawable.atvar_default)
            }
        }
        if(user!=null){
            binding.fragLUser.setBackgroundResource(R.drawable.main_block_bg)
            binding.fragLUserInfo.visibility = ViewGroup.VISIBLE
            binding.fragLlUserAddress.visibility = ViewGroup.VISIBLE
            binding.fragTvUserCard.text = "卡号：${user?.cardNo}"
            binding.fragTvUserPoint.text = user?.totalPoint.toString()
            binding.fragTvUserBuilding.text = user?.nickName
            binding.fragTvUserAddress.text = "地址：${user?.detailedAddress}"
            if (user?.avatar != null) {
                Glide.with(this)
                    .load(user?.avatar)
                    .transform(RoundedCorners(16))
                    .into(binding.frageImgAvatar)
            } else {
                binding.frageImgAvatar.setImageResource(R.drawable.atvar_default)
            }
        }
    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment UserFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            UserFragment(null).apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}